Arrange array element in right and left order starting from least element

62

Arrange array element in right and left order starting from least element -

var N = Convert.ToInt32(Console.ReadLine());
var arr = new List<int>();
for (int i = 0; i < N; i++)
{
    arr.Add(Convert.ToInt32(Console.ReadLine()));
}

arr.Sort();

var newArr = new int[arr.Count];
var temp = true;
var right = new Queue<int>();
var left = new Stack<int>();    

for (int i = 1; i < N; i++)
{
    if(temp)
    {
        temp = false;
        right.Enqueue(arr[i]);
    }
    else
    {
        temp=true;
        left.Push(arr[i]);
    }
}
var j = 0;

while(true)
{
    while(left.Count > 0)
    {
        newArr[j++] = left.Pop();
    }
    newArr[j++] = arr[0];
    while (right.Count > 0)
    {
        newArr[j++] = right.Dequeue();
    }
    break;
}
foreach (var item in newArr)
{
    Console.Write(item + " ");
}

Comments

Submit
0 Comments